package 中等;

import java.util.Arrays;

public class 删除被覆盖区间 {


    public static void main(String[] args) {
        int[][] intervals ={{1,4},{3,6},{2,8}} ;
        System.out.println(removeCoveredIntervals(intervals));

    }

    public static int removeCoveredIntervals(int[][] intervals) {
        Arrays.sort(intervals,(a,b)->{
            //如果起点相同,就按终点降序排列
            if (a[0]==b[0]){
                return b[1] - a[1];
            }
            //按起点升序排列
            return a[0]-b[0];
        } );

        int right = intervals[0][1];
        int res = 0;

        for (int i = 1; i < intervals.length; i++) {
            //这个区间被覆盖了
            if (intervals[i][1]<=right){
               res++;
            }else{
                //2个区间有重合,扩大范围
                right = intervals[i][1];
            }
        }

        return intervals.length-res;
    }
}
